<?php
namespace Todo\Service;

/**
 * Created by PhpStorm.
 * User: yepanpan
 * Date: 2016/8/8
 * Time: 17:08
 */
class SearchService extends BaseService
{
    private static $__INSTANCE = null;
    protected $_xs = null;

    public function __construct($app)
    {
        define('XS_APP_ROOT', APP_PATH.'../data/xunsearch/');
        import('XS', APP_PATH.'../framework/Vendor/XunSearch/', '.php');
        $this->_xs = new \XS($app);
    }

    public static function getInstance($app){
        if(!self::$__INSTANCE) self::$__INSTANCE = new SearchService($app);

        return self::$__INSTANCE;
    }

    public function add($data){
        $index = $this->_xs->getIndex();
        // 创建文档对象
        $doc = new \XSDocument;
        $doc->setFields($data);

        // 添加到索引数据库中
        $index->add($doc);
    }

    public function search($key){
        $search = $this->_xs->getSearch();
        return $search->search($key);
    }
}